/**
 * Created by yangh on 2016/09/14.
 */
var main = {};
var myScroll;
var canLoad=true;
(function ($) {
	main.init = function () {
		/* 	*/
		document.addEventListener('touchmove', function (e) {
			e.preventDefault();
		}, false);
		main.scroll();
	
		
		$(".contentBar li").on("click",function(){
        	//样式
        	$(".contentBar li").removeClass("active");
            $(this).addClass("active");
            $("#topicRecList").html("");
            $("#topicList").html("");
        	//隐藏域
        	var boardid = $(this).data("boardid");
        	$("#boardid").val(boardid);
        	
        	//初始化精华帖子
			main.loadData("#topicRecList",1,1);
			//初始化普通帖子
			main.loadData("#topicList",0,1);
        })
	};

	/**
	 * @func move()
	 * @desc 监听滚动条的位置，异步加载列表
	 */
	function move() {
		if(!canLoad){
    		return;
    	}
		if (this.y < main.max - 80) {
			canLoad=false;
			$("#tip").hide();
			$("#loading").show();
			
			//加载普通帖子
			main.loadData("#topicList",0,0);
			$("#loading").hide();
			$("#tip").show();
		}
	}

    /** 
     * function loadData 加载数据
     */
    main.loadData = function(target,isRecommend,isReplace){
    	//ajax begin
    	 var boardid=$("#boardid").val();
    	 var index = -1;
    	 if($(target+" > li").length!=0){
    		 index=$(target+" > li:last").data("id");
    	 }
         var html = "";
         $.ajax({
     		type:"post",
     		async:false, 
        	url:path+"/bbs/topic/more",
        	data:{
        		boardid:boardid,
        		index:index,
        		isRecommend:isRecommend
        	},
     		success:function(data){	
     				if('000' == data.code){
     				
     				//迭代数据
     				if(isRecommend==0){
	     				var html = "";
	     				for (var i = 0; i < data.result.length; i++) {
	     					var topic = data.result[i];
	     					var infoUrl = path+"/bbs/topic/"+topic.id;
	     					
	     					html +="<li data-id='"+topic.id+"'>";
	     					//头像
	     					html +="<div class=\"log\">";
	 						html +="<a href='"+infoUrl+"' class=\"detailsMes\">";
	 						html +="<div class=\"detailsImg\">";
	 						html +="<img src='"+topic.headimgurl+"'>";
	 						html +="</div>";
	 						html +="<div class=\"detailsCon\">";
	 						html +="<h3>"+topic.nickname+"</h3>";
							html +="<p class=\"data\">"+topic.createTime+"</p>";
							html +="</div>";
	 						html +="<div class=\"detailsRight\">";
	 						html +="<span class=\"fa fa-angle-right\"></span>";
	 						html +="</div>";
	 						html +="</a>";
	 						
	 						//详情
	 						html+="<a href='"+infoUrl+"'>";
	 						html +="<div class=\"details\">";
	 						html +="<h3>"+topic.name+"</h3>";
		 					html +="<p class=\"textDesc\">"+topic.content+"</p>";
		 					html +="</div>";
		 					html +="</div>";
		 					html+="</a>";
		 					
		 					//图片
		 					html +="<div class=\"detailsMes1\">";
	 						html +="<ul class=\"imgs\">";
	 						for(var j=0; j<topic.topicPicList.length ;j++){
							var item = topic.topicPicList[j];
	 						html +="<li>";
	 						html +="<a href=\"javascript:;\">";
	 						html +="<img src='"+item.picurl+"'>";
	 						html +="</a>";
							html +="</li>";
	 						}
							html +="</ul>";
							
							
							//按钮
							html +="<div class=\"button\">";
	 						html +="<a style='border:0px;' href='"+infoUrl+"'>";
	 						html +="<span class=\"fa fa-commenting-o\"></span>评论";
	 						html +="</a>";
	 						/*
	 						html +="<a href=\"javascript:;\">";
 							html +="<span class=\"fa fa-thumbs-o-up\"></span>点赞";
	 						html +="</a>";
	 						*/
							html +="</div>";
	 						html +="</div>";
							html +="</li>";
	     				}	
     				}else{
     					var html = "";
	     				for (var i = 0; i < data.result.length; i++) {
	     					var topic = data.result[i];
	     					var infoUrl = path+"/bbs/topic/"+topic.id;
	     					html+="<a href='"+infoUrl+"'>";
	     					html+="<li class=\"piece\">"+topic.name;
	     					html+="<span class=\"tip\">精华</span>";
	     					html+="</li>";
	     					html+="</a>";
	     				}
     				}
     				
     				//初始化或者加载
     				if(isReplace==1){
     					$(target).html(html);
     				}else{
     					$(target).append(html);
     				}
     				if(data.result.length>=10){
		  	            $("#tip").show();
		  	            canLoad=true;
  		            }
     				myScroll.refresh();
     		        main.max = myScroll.maxScrollY;
     		        console.log(main.max);
     			}
     		}
     	})
   		//ajax end
    }
    
	/**
	 * @func main.scroll()
	 * @desc 配置iscroll插件
	 */
	main.scroll = function () {
		myScroll = new IScroll("#wrapper", {probeType: 3, mouseWheel: true, click: true, preventDefault: false});

		main.max = myScroll.maxScrollY;
		/*console.log(main.max);*/

		myScroll.on("scroll", move);
	}
})(jQuery);

main.init();
//初始化精华帖子
main.loadData("#topicRecList",1,0);
//初始化普通帖子
main.loadData("#topicList",0,0);

